import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.example.linmiaosheng.draw.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by LinMiaosheng on 2018/3/30.
 */

public class Myview extends Activity {
    private ImageView iv;
    private Bitmap bitsrc;
    private Bitmap bitcopy;
    private Canvas canvas;
    private Paint paint;
    private int startX;
    private int startY;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        setBitmap();

        iv.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        startX = (int) event.getX();
                        startY = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int x = (int) event.getX();
                        int y = (int) event.getY();
                        canvas.drawLine(startX, startY, x, y, paint);
                        startX = x;
                        startY = y;
                        iv.setImageBitmap(bitcopy);
                        break;
                    case MotionEvent.ACTION_UP:
                        break;

                }
                return true;
            }
        });
    }

    public void setBitmap() {
        bitsrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        bitcopy = Bitmap.createBitmap(bitsrc.getWidth(), bitsrc.getHeight(),
                bitsrc.getConfig());
        paint = new Paint();
        canvas = new Canvas(bitcopy);
        canvas.drawBitmap(bitsrc, new Matrix(), paint);
        iv.setImageBitmap(bitcopy);
    }

    public void red(View view){
        paint.setColor(Color.RED);
    }
    public void green(View view){
        paint.setColor(Color.GREEN);
    }
    public void brush(View view){
        paint.setStrokeWidth(8);
    }
    public void cancel(View view){
        setBitmap();
    }
    public void eraser(View view){
        paint.setColor(Color.rgb(243,243,243));

        paint.setStrokeWidth(30);
    }

    public void save(View view){
        String path = Environment.getExternalStorageDirectory() + "/" + "2.png";
        File file = new File(path);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitcopy.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
        intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
        sendBroadcast(intent);
    }

}

